﻿using System;
using SedionFormDesigner.Serializer.BasicStruct;
using System.Xml.Linq;
using CommonLibrary.DataModel.Form;

namespace SedionFormDesigner.Serializer
{
    public class SeriEntyAndXml
    {
        private SeriEntyAndXml() { }
        private static SeriEntyAndXml instance = null;
        public static SeriEntyAndXml Instance
        {
            get
            {
                if (instance == null)
                    instance = new SeriEntyAndXml();
                return instance;
            }
        }

        public FormEntity SerializeXmlToFormEntity(XElement xml)
        {
            FormEntity formEntity = new FormEntity();

            var formName = xml.Attribute("FormName").Value;
            formEntity.name = formName;
            var chilElems = xml.Elements();

            foreach (var chilElem in chilElems)
            {
                foreach (XElement f in chilElem.Elements())
                {
                    //if (f.Element("Type").Value == "Form")
                    //{
                    //    formEntity.name = f.Element("Name").Value;

                    //}
                    //else 
                    var ffe = getFormFieldEnty();
                    if (f.Element("Type").Value == "BD_Label")
                    {
                        setBD_Lable(ref ffe, f);
                    }
                    else if (f.Element("Type").Value == "BD_TextBox")
                    {
                        setBD_TextBox(ref ffe, f);
                    }
                    else if (f.Element("Type").Value == "BD_RichTextBox")
                    {
                        setBD_RichTextBox(ref ffe, f);
                    }
                    else if (f.Element("Type").Value == "BD_Button")
                    {
                        setBD_Button(ref ffe, f);
                    }
                    else if (f.Element("Type").Value == "BD_RadioButton")
                    {
                        setBD_RadioButton(ref ffe, f);
                    }
                    else if (f.Element("Type").Value == "BD_CheckBox")
                    {
                        setBD_CheckBox(ref ffe, f);
                    }
                    else if (f.Element("Type").Value == "BD_ComboBox")
                    {
                        setBD_ComboBox(ref ffe, f);
                    }
                    else if (f.Element("Type").Value == "BD_PictureBox")
                    {
                        setBD_PictureBox(ref ffe, f);
                    }
                    else if (f.Element("Type").Value == "BD_DataGridView")
                    {
                        setBD_DataGridView(ref ffe, f);
                    }
                    else if (f.Element("Type").Value == "BD_DateTimePicker")
                    {
                        setBD_DateTimePicker(ref ffe, f);
                    }
                    else if (f.Element("Type").Value == "BD_Currency")
                    {
                        setBD_Currency(ref ffe, f);
                    }
                    else if (f.Element("Type").Value == "BD_Horizon")
                    {
                        setBD_Horizon(ref ffe, f);
                    }
                    else if (f.Element("Type").Value == "BD_Vertical")
                    {
                        setBD_Vertical(ref ffe, f);
                    }
                    formEntity.formfieldEntityList.Add(ffe);
                }
            }

            return formEntity;
        }


        private FormFieldEntity getFormFieldEnty()
        {
            FormFieldEntity ffe = new FormFieldEntity()
            {
                descn = "",
                fieldtype = "string",
                form_id = -1,
                formfield_id = -1,
                inputfrom = "",
                isquery = 0,
                fromvalue = "",
                valuescale = "",
                fromparams = "",
            };

            return ffe;
        }

        private void setBD_Lable(ref FormFieldEntity ffe, XElement xml)
        {
            ffe.valuedefault = xml.Element("Text").Value;
            ffe.name = xml.Element("Name").Value;
            ffe.webtype = "label";
            ffe.formtype = "Label";
        }

        private void setBD_Button(ref FormFieldEntity ffe, XElement xml)
        {
            ffe.valuedefault = xml.Element("Text").Value;
            ffe.name = xml.Element("Name").Value;
            ffe.webtype = "button";
            ffe.formtype = "Button";
        }

        private void setBD_TextBox(ref FormFieldEntity ffe, XElement xml)
        {
            ffe.valuedefault = xml.Element("Text").Value;
            ffe.name = xml.Element("Name").Value;
            ffe.formtype = "TextBox";
            ffe.webtype = "text";
            ffe.ifwfjudge = string.IsNullOrEmpty(xml.Element("ArgName").Value) ? "" : xml.Element("ArgName").Value;
        }

        private void setBD_RichTextBox(ref FormFieldEntity ffe, XElement xml)
        {
            ffe.valuedefault = xml.Element("Text").Value;
            ffe.name = xml.Element("Name").Value;
            ffe.webtype = "textarea";
            ffe.formtype = "RichTextBox";
        }

        private void setBD_CheckBox(ref FormFieldEntity ffe, XElement xml)
        {
            ffe.valuedefault = xml.Element("Text").Value;
            ffe.name = xml.Element("Name").Value;
            ffe.webtype = "checkbox";
            ffe.formtype = "CheckBox";
        }

        private void setBD_RadioButton(ref FormFieldEntity ffe, XElement xml)
        {
            ffe.valuedefault = xml.Element("Text").Value;
            ffe.name = xml.Element("Name").Value;
            ffe.webtype = "radio";
            ffe.formtype = "RadioButton";
        }

        private void setBD_ComboBox(ref FormFieldEntity ffe, XElement xml)
        {
            ffe.valuedefault = xml.Element("Text").Value;
            ffe.name = xml.Element("Name").Value;
            ffe.webtype = "select";
            ffe.formtype = "ComboBox";
        }

        private void setBD_PictureBox(ref FormFieldEntity ffe, XElement xml)
        {
            ffe.valuedefault = xml.Element("Text").Value;
            ffe.name = xml.Element("Name").Value;
            ffe.webtype = "image";
            ffe.formtype = "PictureBox";
        }

        private void setBD_DataGridView(ref FormFieldEntity ffe, XElement xml)
        {
            ffe.valuedefault = xml.Element("Text").Value;
            ffe.name = xml.Element("Name").Value;
            ffe.webtype = "table";
            ffe.formtype = "DataGridView";

        }

        private void setBD_DateTimePicker(ref FormFieldEntity ffe, XElement xml)
        {
            ffe.valuedefault = xml.Element("Text").Value;
            ffe.name = xml.Element("Name").Value;
            ffe.webtype = "text";
            ffe.formtype = "DateTimePicker";
        }

        private void setBD_Currency(ref FormFieldEntity ffe, XElement xml)
        {
            ffe.valuedefault = xml.Element("Text").Value;
            ffe.name = xml.Element("Name").Value;
            ffe.webtype = "text";
            ffe.formtype = "Currency";
        }

        private void setBD_Horizon(ref FormFieldEntity ffe, XElement xml)
        {
            ffe.valuedefault = xml.Element("Text").Value;
            ffe.name = xml.Element("Name").Value;
            ffe.webtype = "div";
            ffe.formtype = "panel";
        }

        private void setBD_Vertical(ref FormFieldEntity ffe, XElement xml)
        {
            ffe.valuedefault = xml.Element("Text").Value;
            ffe.name = xml.Element("Name").Value;
            ffe.webtype = "div";
            ffe.formtype = "panel";
        }
    }
}
